package org.lagou.Dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.lagou.Dao.IssueDao;
import org.lagou.bean.IssueBean;
import org.lagou.resultsethandler.IssueBeanResultSetHandler;

public class IssueDaoImpl extends BaseDao implements IssueDao {

	public List<IssueBean> getIssueAll() {
		try {
			return query.query("select * from issue", new BeanListHandler<IssueBean>(IssueBean.class));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public List<IssueBean> getIssuePagin(int pageIndex, int pageSize) {
		try {
			return query.query("select * from issue limit ?,?", new BeanListHandler<IssueBean>(IssueBean.class),
					(pageIndex - 1) * pageSize, pageSize);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public long getIssueCount() {
		try {
			return query.query("select count(1) from issue", new ScalarHandler<Long>());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	public int addIssue(IssueBean info) {
		try {
			return query.update(
					"insert into issue(companyid,name,category,dept,nature,minsalary,maxsalary,city,experience,education,temptation,summary,address,email,companyname,date,del) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
					info.getCompanyid(), info.getName(), info.getCategory(), info.getDept(), info.getNature(),
					info.getMinsalary(), info.getMaxsalary(), info.getCity(), info.getExperience(), info.getEducation(),
					info.getTemptation(), info.getSummary(), info.getAddress(), info.getEmail(), info.getCompanyname(),
					info.getDate(), info.getDel());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	public int updateIssue(IssueBean info) {
		try {
			return query.update(
					"update issue set companyid=?,name=?,category=?,dept=?,nature=?,minsalary=?,maxsalary=?,city=?,experience=?,education=?,temptation=?,summary=?,address=?,email=?,companyname=?,date=?,del=? where id=?",
					info.getCompanyid(), info.getName(), info.getCategory(), info.getDept(), info.getNature(),
					info.getMinsalary(), info.getMaxsalary(), info.getCity(), info.getExperience(), info.getEducation(),
					info.getTemptation(), info.getSummary(), info.getAddress(), info.getEmail(), info.getCompanyname(),
					info.getDate(), info.getDel(), info.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	public int deleteIssueById(int id) {
		try {
			return query.update("delete from issue where id=?", id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public List<IssueBean> getIssueAndCompany() {
			try {
				return query.query("select l.id,l.name,l.minsalary,l.maxsalary,l.city,l.education,l.experience,l.address,l.summary,l.email,l.temptation,l.companyname,c.field,l.companyid,c.userid,c.stage,c.scale,c.generator,c.tag,c.logo,c.url  from issue l,company c where l.companyid=c.id", new IssueBeanResultSetHandler());
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
	}

	@Override
	public int updatedel(int id) {
		try {
			return query.update("update issue set del=1 where id=?",id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public List<IssueBean> SelectIssueAndCompanyByName(String name) {
		try {
			return query.query("select l.id,l.name,l.minsalary,l.maxsalary,l.city,l.education,l.experience,l.address,l.summary,l.email,l.temptation,l.companyname,c.field,l.companyid,c.userid,c.stage,c.scale,c.generator,c.tag,c.logo,c.url  from issue l,company c where l.companyid=c.id and l.name=?", new IssueBeanResultSetHandler(),name);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<IssueBean> SelectIssueAndCompanyByID(int id) {
		try {
			return query.query("select l.*,c.field,l.companyid,c.userid,c.stage,c.scale,c.generator,c.tag,c.logo,c.url  from issue l,company c where l.companyid=c.id and l.id=?", new IssueBeanResultSetHandler(),id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<IssueBean> getIssueByCompanyid(int companyid) {
		try {
			return query.query(
					"select * from issue where companyid=?",new BeanListHandler<IssueBean>(IssueBean.class), companyid);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}

}
